package com.liandu.controller;

import com.liandu.common.Common;
import com.liandu.common.JResponse;
import com.liandu.entity.Account;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "登录相关")
@RestController
@RequestMapping("/base")
public class BaseController {

    @ApiOperation("1 登录")
    @PostMapping("/login")
    public JResponse login(@RequestBody Account account,@ApiIgnore HttpSession session){
        if (session.getAttribute(Common.ACCOUNT) != null && session.getAttribute(Common.ACCOUNT).equals(Common.account)) {
            return JResponse.successful("已登录",201);
        }
        if (account.getUserName().equals( Common.account.getUserName()) && account.getPassword().equals(Common.account.getPassword())) {
            session.setAttribute(Common.ACCOUNT,account);
            return JResponse.successful(Common.account);
        } else {
            return JResponse.fail("账号密码错误");
        }
    }

    @ApiOperation("2 登出")
    @PostMapping("/logOut")
    public JResponse logOut(@ApiIgnore HttpSession session){
        session.invalidate();
        return JResponse.successful();
    }


}
